<?php
class SiteAdmin_ObjectTypeController extends SiteAdminController
{
		
   	public function indexAction()
    {
    	$this->view->otypes = Obj_ObjectType::getObjectTypes();    	
    }
    
   	public function editAction()
    {
		$id = (int) $this->getRequest()->getParam('id', 0);
		$addnew = intval($this->getRequest()->getParam('addnew', 0)) === 1 ? true : false;
		$arObjectType = Obj_ObjectType::getObjectType($id);
		if ($addnew || $arObjectType === false) {
    		$this->view->otype = array(
    			'ID' => 0,
    			'Name' => '',
    			'Active' => 0,
    		);
    	}
    	else {
    		$this->view->otype = $arObjectType;
    	}    	
    }

   	public function deleteAction()
    {
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	Obj_ObjectType::delObjectType($id);
    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_OBJECT_TYPE);
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index'))), true, false);  	
    }

    public function saveAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$arObjectType = array();
    	$id = (int) $this->getRequest()->getParam('ID', 0);
    	$arObjectType['Name'] = trim($this->getRequest()->getParam('Name', ''));
    	if ($arObjectType['Name'] == '') {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Тип объекта должен иметь название!')), true, false);
    	} elseif(strlen($arObjectType['Name']) > 255){
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Слишком длинное название типа объекта!')), true, false);
    	}
    	$arObjectType['Active'] = intval($this->getRequest()->getParam('active', 0));
    	
    	$otid = Obj_ObjectType::saveObjectType($arObjectType, $id);
    	if ($arObjectType['Active'] === 1) Obj_System_SearchIndex::write($otid, CONTENT_TYPE_OBJECT_TYPE, $arObjectType['Name']);
    	else Obj_System_SearchIndex::delete($otid, CONTENT_TYPE_OBJECT_TYPE);    

		$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index'))), true, false);
    }

}